

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/images/head.jpg">
  <link rel="icon" type="image/png" href="/images/head.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="1|2|3|4|5">
  <meta name="author" content="盖世神论">
  <meta name="keywords" content="">
  <title>嵌入式Linux应用程序开发 - My Blog</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.1.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"gaishishenlun.github.io","root":"/","version":"1.8.5","typing":{"enable":true,"typeSpeed":64,"cursorChar":"_","loop":false},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"copy_btn":true,"image_zoom":{"enable":true},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>盖世神伦</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archive/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/books/">
                <i class="iconfont icon-books"></i>
                书签
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="嵌入式Linux应用程序开发">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-04-15 19:00" pubdate>
        2021-04-15 19:00:17
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.4k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      56
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">嵌入式Linux应用程序开发</h1>
            
            <div class="markdown-body">
              <h1 id="嵌入式Linux应用程序开发期中考试"><a href="#嵌入式Linux应用程序开发期中考试" class="headerlink" title="嵌入式Linux应用程序开发期中考试"></a>嵌入式Linux应用程序开发期中考试</h1><h2 id="第一章-搭建嵌入式Linux开发环境"><a href="#第一章-搭建嵌入式Linux开发环境" class="headerlink" title="第一章 搭建嵌入式Linux开发环境"></a>第一章 搭建嵌入式Linux开发环境</h2><h3 id="1-1、构建嵌入式Linux交叉开发环境"><a href="#1-1、构建嵌入式Linux交叉开发环境" class="headerlink" title="1.1、构建嵌入式Linux交叉开发环境"></a>1.1、构建嵌入式Linux交叉开发环境</h3><ul>
<li><p>交叉编译器的安装包括binutils、gcc、glibc、glibc-linuxthreads</p>
</li>
<li><p>嵌入式平台社区或厂商一般会提供在各种平台上测试通过的交叉编译器</p>
</li>
</ul>
<h3 id="1-2、Bootloader"><a href="#1-2、Bootloader" class="headerlink" title="1.2、Bootloader"></a>1.2、Bootloader</h3><ul>
<li><p>Bootloader是在操作系统运行之前执行的一段小程序。通过它可以初始化硬件设备、建立内存空间的映射表，从而建立适当的系统软、硬件环境，为最终调用操作系统内核做好准备。</p>
</li>
<li><p>Bootloader是基于特定硬件平台来实现的，因此，几乎不可能为所有的嵌入式系统都建立一个通用的Bookloader,不同的处理器架构有不同的Bootloader.</p>
</li>
<li><p>U-Boot同时支持PowerPC、ARM、MIPS和X86等体系结构</p>
</li>
</ul>
<h3 id="1-4、嵌入式文件系统的构建"><a href="#1-4、嵌入式文件系统的构建" class="headerlink" title="1.4、嵌入式文件系统的构建"></a>1.4、嵌入式文件系统的构建</h3><ul>
<li>busybox是构造文件系统最常用的软件工具包</li>
</ul>
<h2 id="第二章-嵌入式文件I-O编程"><a href="#第二章-嵌入式文件I-O编程" class="headerlink" title="第二章 嵌入式文件I/O编程"></a>第二章 嵌入式文件I/O编程</h2><h3 id="2-2、Linux文件I-O系统概述"><a href="#2-2、Linux文件I-O系统概述" class="headerlink" title="2.2、Linux文件I/O系统概述"></a>2.2、Linux文件I/O系统概述</h3><ul>
<li><p>Linux的文件系统由两层结构构建：<strong>第一层</strong>是虚拟文件系统(VFS)，<strong>第二层</strong>是各种不同的具体文件系统。</p>
</li>
<li><p>VFS就是把各种具体的文件系统的公共部份抽取出来，形成一个抽象层，是系统内核的一小部分，它位于用户程序和具体文件系统之间。</p>
</li>
<li><p>通过这样的方式，VFS就对用户屏蔽了底层文件系统的实现细节和差异。</p>
</li>
<li><p>一个进程启动时，都会打开3个文件：标准输入、标准输出和标准出错处理。这3个文件对应的文件描述符分别为0、1和2（也就是宏替换STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO，鼓励读者使用这些宏替换）。</p>
</li>
</ul>
<h3 id="2-3、底层文件I-O"><a href="#2-3、底层文件I-O" class="headerlink" title="2.3、底层文件I/O"></a>2.3、底层文件I/O</h3><ul>
<li>open()、close()、read()、write()、lseek</li>
<li>int open(const char *pathname, int flags, int perms)</li>
<li>off_t lseek(int fd, off_t offset, int whence)<ul>
<li>whence：SEEK_SET、SEEK_CUR、SEEK_END</li>
</ul>
</li>
<li>记录锁又可分为读取锁和写入锁，其中读取锁又称为共享锁</li>
<li>I/O处理的模型有以下5种<ul>
<li>阻塞I/O模型</li>
<li>非阻塞I/O模型</li>
<li>I/O多路转接模型</li>
<li>信号驱动I/O模型</li>
<li>异步I/O模型</li>
</ul>
</li>
</ul>
<h3 id="2-4、串口配置"><a href="#2-4、串口配置" class="headerlink" title="2.4、串口配置"></a>2.4、串口配置</h3><ul>
<li>1、保存原先串口配置</li>
<li>2、激活选项</li>
<li>3、设置波特率</li>
<li>4、设置字符大小</li>
<li>5、设置奇偶校验位</li>
<li>6、设置停止位</li>
<li>7、设置最少字符和等待时间</li>
<li>8、清楚串口缓冲</li>
<li>9、激活配置</li>
</ul>
<h3 id="2-5、标准I-O编程"><a href="#2-5、标准I-O编程" class="headerlink" title="2.5、标准I/O编程"></a>2.5、标准I/O编程</h3><ul>
<li>标准I/O编程操作是基于流缓冲的，符合ANSIC的标准I/O处理。</li>
</ul>
<h2 id="第三章-嵌入式Linux多任务编程"><a href="#第三章-嵌入式Linux多任务编程" class="headerlink" title="第三章 嵌入式Linux多任务编程"></a>第三章 嵌入式Linux多任务编程</h2><h3 id="3-1、Linux下多任务机制的介绍"><a href="#3-1、Linux下多任务机制的介绍" class="headerlink" title="3.1、Linux下多任务机制的介绍"></a>3.1、Linux下多任务机制的介绍</h3><ul>
<li>进程是指一个具有独立功能的程序在某个数据集上的一次动态执行过程，它是系统进行资源分配和调度的基本单元</li>
<li>进程具有并发性、动态性、独立性和异步性等主要特性。</li>
<li>进程的终止<ul>
<li>Linux首先把终结的进程设置为僵尸状态，这是，进程无法投入运行，它的存在只为父进程提供信息，申请死亡。父进程得到信息后，开始调用wait()函数族，最后终止子进程，子进程占用的所有资源被全部释放。</li>
</ul>
</li>
<li>进程是系统中程序执行和资源分配的基本单位。</li>
<li>为了进一步减少处理机的空转时间，支持多处理器及减少上下文切换开销，进程在演化中出现了另一个概念——<strong>线程</strong>。</li>
<li>线程是进程内独立的一条运行路线，是处理器调度的最小单元，也可以成为轻量级进程。线程可以对进程的内存空间和资源进行访问，并于同一个进程中的其他线程共享。因此，线程的上下文切换开销比创建进程小得多。</li>
<li>线分为：用户级线程、轻量级线程、内核线程</li>
</ul>
<h3 id="3-2、进程控制编程"><a href="#3-2、进程控制编程" class="headerlink" title="3.2、进程控制编程"></a>3.2、进程控制编程</h3><ul>
<li><p>**fork()**函数用于从已存在的进程中创建一个新进程。新进程成为子进程，原进程称为父进程。</p>
<ul>
<li>fork()函数得到的子进程是父进程的一个复制品，它从父进程处继承了整个进程的地址空间，包括进程上下文、代码段、进程堆栈、内存信息、打开的文件描述符、信号控制设定、进程优先级、进程组号、当前工作目录、跟目录、资源限制和控制终端等，而子进程所独有的只有它的进程号、资源使用和计时器等。</li>
</ul>
</li>
<li><p>fork函数的简单示例程序如下：</p>
</li>
</ul>
<pre><code class="hljs C"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
    
    <span class="hljs-keyword">pid_t</span> pid;
    result = fork();
    
    <span class="hljs-keyword">if</span> (<span class="hljs-number">-1</span> == result) &#123;
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Fork error\n&quot;</span>);
    &#125;
    <span class="hljs-comment">// 返回值为0代表子进程</span>
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-number">0</span> == result) &#123;
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;The returned value is %d\nIn child process!!\nMy PID is %d\n&quot;</span>, result, getpid());
    &#125;
    <span class="hljs-comment">// 返回值大于0代表父进程</span>
    <span class="hljs-keyword">else</span> &#123;
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;The returned value is %d\nIn father process!!\nMy PID is %d\n&quot;</span>, result, getpid());
    &#125;
    
    <span class="hljs-keyword">return</span> result;
&#125;</code></pre>



<ul>
<li><strong>exec</strong>()函数使用实例：</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
    <span class="hljs-keyword">if</span> (fork() == <span class="hljs-number">0</span>) &#123;
        <span class="hljs-keyword">if</span> (ret = execlp(<span class="hljs-string">&quot;ps&quot;</span>, <span class="hljs-string">&quot;ps&quot;</span>, <span class="hljs-string">&quot;-ef&quot;</span>, <span class="hljs-literal">NULL</span>)) &lt; <span class="hljs-number">0</span>) &#123;	<span class="hljs-comment">// 调用execlp()函数，执行&quot;ps -ef&quot;命令</span>
            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Excelp error\n&quot;</span>);
        &#125;
    &#125;
&#125;</code></pre>



<ul>
<li><p>**_exit()**函数的作用是：直接使进程停止运行，清除其使用的内存空间，并清除其在内核中的各种数据结构</p>
</li>
<li><p>**exit()**函数在_exit()函数的基础上做了一些包装，<u>在终止当前进程之前检查该进程打开过哪些文件，把文件缓冲区中的内容写回文件(清理I/O缓冲区)。</u></p>
</li>
<li><p>以下为exit()与_exit()函数使用实例</p>
</li>
</ul>
<pre><code class="hljs C"><span class="hljs-comment">/* exit.c */</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Using exit...\n&quot;</span>);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;This is the content in buffer\n&quot;</span>);
    <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
&#125;</code></pre>

<pre><code class="hljs C"><span class="hljs-comment">/* _exit.c */</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Using _exit...\n&quot;</span>);
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;This is the content in buffer\n&quot;</span>);
    _exit(<span class="hljs-number">0</span>);
&#125;</code></pre>

<ul>
<li>运行效果：</li>
</ul>
<pre><code class="hljs shell"><span class="hljs-meta">$</span><span class="bash"> ./<span class="hljs-built_in">exit</span>	<span class="hljs-comment"># exit()函数的运行效果</span></span>
Using exit...
This is the content in buffer $

<span class="hljs-meta">$</span><span class="bash"> ./_exit	<span class="hljs-comment"># _exit()函数的运行效果</span></span>
Using _exit...
<span class="hljs-meta">$</span></code></pre>



<ul>
<li><p>wait()函数用于使父进程(也就是调用wait()的进程)阻塞</p>
</li>
<li><p>waitpid()的作用和wait()一样，但可以提供一个非阻塞版本的wait()功能</p>
<ul>
<li>pid_t waitpid(pid_d pid, int *status int options)<ul>
<li>options：WHOHANG（若由pid指定的子进程没有结束，则waitpid()不阻塞而立即返回，返回值为0</li>
</ul>
</li>
</ul>
</li>
<li><p>守护进程(daemon进程)，是Linux中的后台服务进程。它是一个生存周期较长的进程，通常独立于控制终端并且周期性地执行某种任务或者等待处理某些发生的事件。守护进程常常在系统引导载入时启动，在系统关闭时终止。</p>
</li>
<li><p>当控制终端被关闭时，相应的进程都会自动关闭，但是守护进程却能够突破这种限制，它从被执行开始运转，知道接收到某种信号或者整个系统关闭时才会退出。如果想让某个进程不因为用户、终端或者其他的变化而受到影响，那么就必须把这个进程变成一个守护进程</p>
</li>
<li><p>编写守护进程</p>
<ul>
<li>1、创建子进程，父进程退出，子进程没有父进程，从而变成一个孤儿进程</li>
<li>2、在子进程中创建新会话</li>
<li>3、改变当前目录为根目录</li>
<li>4、重设文件权限掩码</li>
<li>5、关闭文件描述符</li>
</ul>
</li>
<li><p>以下是实现守护进程的完整实例</p>
</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;fcntl.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/wait.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
    
    <span class="hljs-keyword">pid_t</span> pid;
    <span class="hljs-keyword">int</span> i, fd;
    <span class="hljs-keyword">char</span> *buf = <span class="hljs-string">&quot;This is a Daemon\n&quot;</span>;
    
    pid = fork();	<span class="hljs-comment">// 第一步：创建子进程</span>
    
    <span class="hljs-keyword">if</span> (pid &lt; <span class="hljs-number">0</span>) &#123;
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Error fork\n&quot;</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
    &#125;
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pid &gt; <span class="hljs-number">0</span>) &#123;
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);	<span class="hljs-comment">// 父进程退出</span>
    &#125;
    
    setsid(); 		<span class="hljs-comment">// 第二步：在子进程中创建新会话</span>
    chdir(<span class="hljs-string">&quot;/&quot;</span>);		<span class="hljs-comment">// 第三步：改变当前目录为根目录</span>
    umask(<span class="hljs-number">0</span>);		<span class="hljs-comment">// 第四步：重设文件权限掩码</span>
    
    <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; getdtablesize(); i++) &#123;
        close(i);	<span class="hljs-comment">// 第五步：关闭文件描述符</span>
    &#125;
    <span class="hljs-comment">// 此时守护进程创建完毕，以下开始进入守护进程工作</span>
    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;
        <span class="hljs-keyword">if</span> ((fd = open(<span class="hljs-string">&quot;/tmp/daemon.log&quot;</span>, O_CREAT|O_WRONLY|O_APPEND, <span class="hljs-number">0600</span>)) &lt; <span class="hljs-number">0</span>) &#123;
            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Open file error\n&quot;</span>);
            <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        &#125;
        write(fd, buf, <span class="hljs-built_in">strlen</span>(buf) + <span class="hljs-number">1</span>);
        close(fd);
        sleep(<span class="hljs-number">10</span>);
    &#125;
    <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
&#125;

<span class="hljs-comment">// 该程序每隔10s就会在对应文件(/tmp/daemon.log)中输入对应内容(This is a Daemon)</span></code></pre>



<h2 id="第四章-嵌入式Linux进程间通信"><a href="#第四章-嵌入式Linux进程间通信" class="headerlink" title="第四章 嵌入式Linux进程间通信"></a>第四章 嵌入式Linux进程间通信</h2><h3 id="4-2、管道通信"><a href="#4-2、管道通信" class="headerlink" title="4.2、管道通信"></a>4.2、管道通信</h3><ul>
<li>无名管道特点：<ul>
<li>只能用于具有亲缘关系的进程之间通信(也就是父子进程或者兄弟进程之间)</li>
<li>是一个半双工的通信模式，具有固定的读端和写端</li>
<li>管道也可以被看成一种特殊的文件，对于它的读写也可以使用普通的read()、write()等函数。但是它不是普通的文件，并不属于其他任何文件系统，并且只存在于内存中。</li>
</ul>
</li>
<li>有名管道(FIFO)<ul>
<li>可以使互不相关的两个进程实现彼此通信</li>
<li>该管道可以通过路径名来指出，并且文件在文件系统中是可见的。在建立了管道之后，两个进程就可以把它当作普通文件进行读写操作，使用非常方便。</li>
<li>严格的遵循先进先出规则，对管道及FIFO的读总是从开始处返回数据，对它们的写则是把数据添加到末尾。他们不支持文件定位操作，如lseek()等。</li>
</ul>
</li>
<li>以下为管道实例：</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;errno.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAX_DATA_LEN            256</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> DELAY_TIME                      1</span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;

        <span class="hljs-keyword">pid_t</span> pid;
        <span class="hljs-keyword">int</span> pipe_fd[<span class="hljs-number">2</span>];
        <span class="hljs-keyword">char</span> buf[MAX_DATA_LEN];
        <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> data[] = <span class="hljs-string">&quot;Pipe Test Program&quot;</span>;
        <span class="hljs-keyword">int</span> real_read, real_write;
        <span class="hljs-built_in">memset</span>((<span class="hljs-keyword">void</span> *)buf, <span class="hljs-number">0</span>, <span class="hljs-keyword">sizeof</span>(buf));
    
        <span class="hljs-keyword">if</span> (pipe(pipe_fd) &lt; <span class="hljs-number">0</span>) &#123;
        <span class="hljs-comment">// 创建管道</span>
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;pipe create error\n&quot;</span>);
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        &#125;
        <span class="hljs-keyword">if</span> ((pid = fork()) == <span class="hljs-number">0</span>) &#123;
        <span class="hljs-comment">// 创建一个子进程</span>
                <span class="hljs-comment">// 子进程关闭写描述符，并通过使子进程暂停1s等待父进程已关闭相应的读描述符</span>
                close(pipe_fd[<span class="hljs-number">1</span>]);
                sleep(DELAY_TIME * <span class="hljs-number">3</span>);
                <span class="hljs-comment">// 子进程读取管道内容</span>
                <span class="hljs-keyword">if</span> ((real_read = read(pipe_fd[<span class="hljs-number">0</span>], buf, MAX_DATA_LEN)) &gt; <span class="hljs-number">0</span>) &#123;
                        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d bytes read from the pipe is &#x27;%s&#x27;\n&quot;</span>, real_read, buf);
                &#125;
                close(pipe_fd[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 关闭子进程读取描述符</span>
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
        &#125;
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pid &gt; <span class="hljs-number">0</span>) &#123;
                <span class="hljs-comment">// 父进程关闭读描述符，并通过使父进程暂停1s等待子进程关闭相应的写描述符</span>
                close(pipe_fd[<span class="hljs-number">0</span>]);
                sleep(DELAY_TIME);

                <span class="hljs-keyword">if</span> ((real_write = write(pipe_fd[<span class="hljs-number">1</span>], data, <span class="hljs-built_in">strlen</span>(data))) != <span class="hljs-number">-1</span>) &#123;
                        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Parent wrote %d bytes: &#x27;%s&#x27;\n&quot;</span>, real_write, data);
                &#125;
                close(pipe_fd[<span class="hljs-number">1</span>]);                      <span class="hljs-comment">// 关闭父进程写描述符</span>
                waitpid(pid, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>);  <span class="hljs-comment">// 收集子进程退出信息</span>
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
        &#125;
&#125;</code></pre>



<h3 id="4-3、信号通信"><a href="#4-3、信号通信" class="headerlink" title="4.3、信号通信"></a>4.3、信号通信</h3><ul>
<li><table>
<thead>
<tr>
<th align="center">信号名</th>
<th align="center">默认操作</th>
</tr>
</thead>
<tbody><tr>
<td align="center">SIGINT</td>
<td align="center">终止</td>
</tr>
<tr>
<td align="center">SIGQUIT</td>
<td align="center">终止</td>
</tr>
<tr>
<td align="center">SIGKILL（无法忽略）</td>
<td align="center">终止</td>
</tr>
<tr>
<td align="center">SIGALRM</td>
<td align="center">终止</td>
</tr>
<tr>
<td align="center">SIGSTOP（无法忽略）</td>
<td align="center">暂停进程</td>
</tr>
</tbody></table>
</li>
<li><p>在子进程中使用raise()函数向自身发送SIGSTOP信号示例：</p>
</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;signal.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/wait.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
        <span class="hljs-keyword">pid_t</span> pid;
        <span class="hljs-keyword">int</span> ret;
        <span class="hljs-comment">// 创建一个子进程</span>
        <span class="hljs-keyword">if</span> ((pid = fork()) &lt; <span class="hljs-number">0</span>) &#123;
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Fork error\n&quot;</span>);
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
        &#125;
        <span class="hljs-keyword">if</span> (pid == <span class="hljs-number">0</span>) &#123;
                <span class="hljs-comment">// 在子进程中使用raise()函数向自己发送SIGSTOP信号，使子进程暂停1s等待</span>
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Child(pid：%d) is waiting for any signal\n&quot;</span>, getpid());
                raise(SIGSTOP);
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
        &#125;
        <span class="hljs-keyword">else</span> &#123;
                <span class="hljs-comment">// 在父进程中收集子进程发出的信号，并调用kill()函数进行相应的操作</span>
                <span class="hljs-keyword">if</span> ((waitpid(pid, <span class="hljs-literal">NULL</span>, WNOHANG)) == <span class="hljs-number">0</span>) &#123;
                        <span class="hljs-keyword">if</span> ((ret == kill(pid, SIGKILL)) == <span class="hljs-number">0</span>) &#123;
                                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Parent kill %d\n&quot;</span>, pid);
                        &#125;
                &#125;
                waitpid(pid, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>);
                <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
        &#125;
&#125;</code></pre>



<ul>
<li><p>alarm()函数具有定时器的功能，定时结束时会向该进程发送SIGALRM信号(默认的系统动作为终止该进程)</p>
</li>
<li><p>pause()函数可以阻塞等待信号</p>
</li>
<li><p>以下为两个函数的实例</p>
</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
        <span class="hljs-comment">// 调用alarm定时器函数</span>
        <span class="hljs-keyword">int</span> ret = alarm(<span class="hljs-number">5</span>);
        pause();
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;I have been waken up.\n&quot;</span>, ret);         <span class="hljs-comment">// 此语句不会被执行</span>
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
&#125;</code></pre>

<ul>
<li>运行效果如下：</li>
</ul>
<pre><code class="hljs bash">$ ./alarm_pause
Alarm clock</code></pre>



<h3 id="4-4、信号量"><a href="#4-4、信号量" class="headerlink" title="4.4、信号量"></a>4.4、信号量</h3><ul>
<li>P操作：如果有可用的资源(信号量值 &gt; 0)，则占用一个资源(给信号量值减1，进入临界区代码)；如果没有可用的资源(信号量值 == 0)，则被阻塞直到系统将资源分配给该进程(进入等待队列，一直等到资源轮到该进程)。</li>
<li>V操作：如果在该信号量的等待队列中有进程在等待资源，则唤醒一个阻塞进程；如果没有进程等待它，则释放一个资源(给信号量值加1)。</li>
<li>在Linux系统中使用信号量通常分为四步：<ul>
<li>1、<strong>创建信号量或获得系统中已存在的信号量</strong>，此时需要调用semget()函数。不同进程通过使用同一个信号量键值来获得同一个信号量。</li>
<li>2、<strong>初始化信号量</strong>，此时使用semctl()函数的SETVAL操作。当使用二维信号量时，通常将信号量初始化为1.</li>
<li>3、<strong>进行信号量的PV操作</strong>，此时调用semop()函数。这一步实现进程间的同步和互斥的核心工作部分。</li>
<li>4、<strong>如果不需要信号量，则从系统中删除它</strong>，此时使用semctl()函数的IPC_RMID操作。需注意的是，在程序中不应该出现对已经被删除的信号量的操作</li>
</ul>
</li>
<li>以下为信号量操作实例</li>
</ul>
<pre><code class="hljs C"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/ipc.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sys/shm.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> DELAY_TIME 3    <span class="hljs-comment">// 为了突出演示效果，等待几秒</span></span>

<span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">semun</span> &#123;</span>
        <span class="hljs-keyword">int</span> val;
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">semid_ds</span> *<span class="hljs-title">buf</span>;</span>
        <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span> *<span class="hljs-built_in">array</span>;
&#125;


<span class="hljs-comment">// 信号量初始化(赋值)函数</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">init_sem</span><span class="hljs-params">(<span class="hljs-keyword">int</span> sem_id, <span class="hljs-keyword">int</span> init_value)</span> </span>&#123;

        <span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">semun</span> <span class="hljs-title">sem_union</span>;</span>
        sem_union.val = init_value;             <span class="hljs-comment">// init_value为初始值</span>

        <span class="hljs-keyword">if</span> (semctl(sem_id, <span class="hljs-number">0</span>, SETVAL, sem_union) == <span class="hljs-number">-1</span>) &#123;
                perror(<span class="hljs-string">&quot;Initialize semaphore&quot;</span>);
                <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
&#125;


<span class="hljs-comment">// 从系统中删除信号量函数</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">del_sem</span><span class="hljs-params">(<span class="hljs-keyword">int</span> sem_id)</span> </span>&#123;

        <span class="hljs-class"><span class="hljs-keyword">union</span> <span class="hljs-title">semun</span> <span class="hljs-title">sem_union</span>;</span>

        <span class="hljs-keyword">if</span> (semctl(sem_id, <span class="hljs-number">0</span>, IPC_RMID, sem_union) == <span class="hljs-number">-1</span>) &#123;
                perror(<span class="hljs-string">&quot;Delete semaphore&quot;</span>);
                <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
&#125;


<span class="hljs-comment">// P操作函数</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sem_p</span><span class="hljs-params">(init sem_id)</span> </span>&#123;
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sembuf</span> <span class="hljs-title">sem_b</span>;</span>
        sem_b.sem_num = <span class="hljs-number">0</span>;                              <span class="hljs-comment">// 单个信号量的编号应该为0</span>
        sem_b.sem_op = <span class="hljs-number">-1</span>;                              <span class="hljs-comment">// 表示P操作</span>
        sem_b.sem_flg = SEM_UNDO;       <span class="hljs-comment">// 系统自动释放将会在系统中残留的信号量</span>

        <span class="hljs-keyword">if</span> (semop(sem_id, &amp;sem_b, <span class="hljs-number">1</span>) == <span class="hljs-number">-1</span>) &#123;
                perror(<span class="hljs-string">&quot;P operation&quot;</span>);
                <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
&#125;


<span class="hljs-comment">// V操作函数</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sem_v</span><span class="hljs-params">(<span class="hljs-keyword">int</span> sem_id)</span> </span>&#123;
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sembuf</span> <span class="hljs-title">sem_b</span>;</span>
        sem_b.sem_num = <span class="hljs-number">0</span>;                              <span class="hljs-comment">// 单个信号量的编号应该为0</span>
        sem_b.sem_op = <span class="hljs-number">1</span>;                                       <span class="hljs-comment">// 表示V操作</span>
        sem_b.sem_flg = SEM_UNDO;       <span class="hljs-comment">// 系统自动释放将会在系统中残留的信号量</span>

        <span class="hljs-keyword">if</span> (semop(sem_id, &amp;sem_b, <span class="hljs-number">1</span>) == <span class="hljs-number">-1</span>) &#123;
                perror(<span class="hljs-string">&quot;V operation&quot;</span>);
                <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
        &#125;
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
&#125;


<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>&#123;
        <span class="hljs-keyword">pid_t</span> result;
        <span class="hljs-keyword">int</span> sem_id;

        sem_id = semget(ftok(<span class="hljs-string">&quot;.&quot;</span>, <span class="hljs-string">&#x27;a&#x27;</span>), <span class="hljs-number">1</span>, <span class="hljs-number">0666</span>|IPC_CREAT);             <span class="hljs-comment">// 创建一个信号量</span>
        init_sem(sem_id, <span class="hljs-number">0</span>);
        <span class="hljs-comment">// 调用fork()函数</span>
        result = fork();

        <span class="hljs-keyword">if</span> (result == <span class="hljs-number">-1</span>) &#123;
                perror(<span class="hljs-string">&quot;Fork&quot;</span>);
        &#125;
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (result == <span class="hljs-number">0</span>) &#123;
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Child process will wait for some seconds...\n&quot;</span>);
                sleep(DELAY_TIME);
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;The returned value is %d in the child process(PID = %d)\n&quot;</span>, result, getpid());
                sem_v(sem_id);
        &#125;
        <span class="hljs-keyword">else</span> &#123;
                sem_p(sem_id);
                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;The returned value is %d in the father process(PID = %d)\n&quot;</span>);
                sem_v(sem_id);
                del_sem(sem_id);
        &#125;
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);
&#125;</code></pre>



<h3 id="4-5、共享内存"><a href="#4-5、共享内存" class="headerlink" title="4.5、共享内存"></a>4.5、共享内存</h3><ul>
<li>共享内存是一种最为高效的进程间通信方式，因为进程可以直接读写内存，不需要任何数据的复制。为了在多个进程间交换信息，内核专门留出了一块内存区，这段内存区可以由需要访问的进程将其映射到自己的私有地址空间。因此，进程就可以直接读写这一内存区而不需要进行数据的复制，<strong>从而大大的提高了效率</strong>。</li>
<li>共享内存的实现分为两个步骤：<ul>
<li>1、创建共享内存</li>
<li>2、映射共享内存</li>
</ul>
</li>
</ul>
<h3 id="4-6、消息队列"><a href="#4-6、消息队列" class="headerlink" title="4.6、消息队列"></a>4.6、消息队列</h3><ul>
<li>消息队列的实现包括<strong>创建</strong>或<strong>打开消息队列</strong>、<strong>添加消息</strong>、<strong>读取消息</strong>、<strong>控制消息队列</strong>四种操作。</li>
</ul>
<h2 id="第五章-嵌入式Linux多线程编程"><a href="#第五章-嵌入式Linux多线程编程" class="headerlink" title="第五章 嵌入式Linux多线程编程"></a>第五章 嵌入式Linux多线程编程</h2><h3 id="5-1、多线程编程"><a href="#5-1、多线程编程" class="headerlink" title="5.1、多线程编程"></a>5.1、多线程编程</h3><ul>
<li>互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。</li>
<li>线程属性<ul>
<li>1、绑定属性</li>
<li>2、分离属性</li>
</ul>
</li>
</ul>
<h2 id="第六章-嵌入式Linux网络编程"><a href="#第六章-嵌入式Linux网络编程" class="headerlink" title="第六章 嵌入式Linux网络编程"></a>第六章 嵌入式Linux网络编程</h2><h3 id="6-2、网络编程基础"><a href="#6-2、网络编程基础" class="headerlink" title="6.2、网络编程基础"></a>6.2、网络编程基础</h3><ul>
<li>套接字类型<ul>
<li>1、流式套接字(SOCK_STREAM) (TCP)</li>
<li>2、数据报套接字(SOCK_DGRAM) (UDP)</li>
<li>3、原始套接字(SOCK_RAW)</li>
</ul>
</li>
<li>sockaddr和sockaddr_in数据结构</li>
</ul>
<pre><code class="hljs C"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr</span> &#123;</span>
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span> sa_family;		<span class="hljs-comment">// 地址族</span>
    <span class="hljs-keyword">char</span> sa_data[<span class="hljs-number">14</span>];				<span class="hljs-comment">// 14字节的协议地址，包含该socket的IP地址和端口号</span>
&#125;;

<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> &#123;</span>
    <span class="hljs-keyword">short</span> <span class="hljs-keyword">int</span> sa_family;			<span class="hljs-comment">// 地址族</span>
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span> <span class="hljs-keyword">int</span> sin_port;	<span class="hljs-comment">// 端口号</span>
    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">in_addr</span> <span class="hljs-title">sin_addr</span>;</span>		<span class="hljs-comment">// IP地址</span>
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> sin_zero[<span class="hljs-number">8</span>];		<span class="hljs-comment">// 填充0以保持与struct sockaddr同样大小</span>
&#125;;

<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">in_addr</span> &#123;</span>
    <span class="hljs-keyword">in_addr_t</span> s_addr;
&#125;</code></pre>

<ul>
<li><p>使用TCP协议socket编程流程图</p>
<pre class="mermaid">  graph TB
    A1[服务器端\nsocket] --> B1[bind] --> C1[listen] --> D1[accept] --> E1[recv/recvfrom] --> F1[send/sendto] ---> G1[close]
    A2[客户端\nsocket] --> Con1[connect] --> ClS[send/sendto] --> E2[recv/recvfrom] --> G2[close]
    A2 --> B2[bind] --> Con1 --> D1
    ClS --> E1
    F1 --> E2</pre></li>
<li><p>使用UDP协议socket编程流程图(图中使用send(recv)或sendto(recvfrom)发送(接收)消息)</p>
</li>
</ul>
<pre class="mermaid">  graph TB
    listen ---> RF1[recvfrom] --> ST1[sendto] --->CLS1
    S1[服务器端\nsocket] ---> listen --> accept --> Re1[recv] --> Se1[send] --->CLS1[close]
    S2[客户端\nsocket] ---> connect --> Se2[send] --> Re2[recv] --> CLS2[close]
    S2 ----> ST2[sendto] ----> RF2[recvfrom] --> CLS2
    connect --> accept
    Se2 --> Re1
    Se1 --> Re2</pre>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/14/mysql-mariadb/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">mysql/mariadb</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/03/24/Arch-for-Magicbook-AMD-R5-2500U/">
                        <span class="hidden-mobile">Arch for Magicbook AMD R5 2500U</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>盖世神伦</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Blog</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  

</footer>
<!-- SCRIPTS -->

  <script  src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":200})
    NProgress.start()
    document.addEventListener('DOMContentLoaded', function() {
      window.NProgress && window.NProgress.inc();
    })
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://cdn.staticfile.org/tocbot/4.12.0/tocbot.min.js" ></script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>












  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>




  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>



</body>
</html>
